package com.baichang.taoshunda.controller;

import com.baichang.framework.controller.BaseController;
import com.baichang.framework.controller.IController;
import com.baichang.framework.controller.IPageController;
import com.baichang.framework.excelption.ServiceException;
import com.baichang.framework.model.Page;
import com.baichang.taoshunda.ControllerConstants;
import com.baichang.taoshunda.dao.PushDao;
import com.baichang.taoshunda.service.PushService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by Administrator on 2017/8/7 0007.
 * 这个是用户类Controller
 */
@Controller
@RequestMapping(ControllerConstants.Push)
public class PushController extends BaseController {

    @Autowired
    private PushDao pushDao;

    @Autowired
    private PushService pushService;


    /**
     * 跳转页面
     */
    @RequestMapping("/findPush")
    public String index(HttpServletRequest request, HttpServletResponse response) {
        return "/taoshunda/push/index";
    }

    /**
     * 展现列表
     */

    @RequestMapping("/pushQuery")
    public void pushQuery(HttpServletRequest request, HttpServletResponse response) {
        initPage(request, response, new IPageController() {
            @Override
            public Page initPage(HttpServletRequest request, HttpServletResponse httpServletResponse) throws ServiceException {
                String tuisongtitle = ServletRequestUtils.getStringParameter(request, "tuisongtitle", "");
                String tuisongren = ServletRequestUtils.getStringParameter(request, "tuisongren", "");
                String xjstart = ServletRequestUtils.getStringParameter(request, "xjstart", "");
                String xjend = ServletRequestUtils.getStringParameter(request, "xjend", "");
                return pushService.pushQuery(nowPage, pageSize, tuisongren, xjstart, xjend, tuisongtitle);
            }
        });
    }

    /**
     * 根据id来进行删除
     */
    @RequestMapping("/deletePushById")
    public void deletePushById(HttpServletRequest request, HttpServletResponse response) {
        init(request, response, new IController() {
            @Override
            public Object initData(HttpServletRequest request, HttpServletResponse httpServletResponse) throws ServiceException {
                int pushId = ServletRequestUtils.getIntParameter(request, "id", 0);
                return pushDao.deletePushById(pushId);
            }
        });
    }


    /**
     * 增加推送消息
     */
    @RequestMapping("/add")
    public void add(HttpServletRequest request, HttpServletResponse response) {
        init(request, response, new IController() {
            @Override
            public Object initData(HttpServletRequest request, HttpServletResponse response) throws ServiceException {
                String title = ServletRequestUtils.getStringParameter(request, "title", "");
                String content = ServletRequestUtils.getStringParameter(request, "content", "");
                String picture = ServletRequestUtils.getStringParameter(request, "picture", "");
                String remark = ServletRequestUtils.getStringParameter(request, "remark", "");//摘要
                String users = ServletRequestUtils.getStringParameter(request, "users", "");
                String tags = ServletRequestUtils.getStringParameter(request, "tags", "");
                int pushtype = ServletRequestUtils.getIntParameter(request, "pushType", 1);//推送类型
                return pushService.pushMsg(picture, title, content, users, remark, pushtype, tags);
            }
        });
    }

    //删除推送的消息
    @RequestMapping("/delete")
    public void delete(HttpServletRequest request, HttpServletResponse response) {
        init(request, response, new IController() {
            @Override
            public Object initData(HttpServletRequest request, HttpServletResponse response) throws ServiceException {
                int id = ServletRequestUtils.getIntParameter(request, "id", 0);

                return pushService.delete(id);
            }
        });
    }
}
